//
//  ZYPopLayerDispose.m
//  DesignMode
//
//  Created by wangzhipan on 2024/11/15.
//

#import "ZYPopLayerDispose.h"
#import "IPopLayerProtocol.h"
#import "ZYPopLayerManager.h"

@interface ZYPopLayerDispose()
@property (nonatomic, strong, nullable) id<IPopLayerRequest> request;
@end

@implementation ZYPopLayerDispose
- (instancetype)initWithRequest:(id<IPopLayerRequest>)request
{
    if (self = [super init]) {
        self.request = request;
    }
    return self;
}

- (void)dispatch
{
    if ([NSThread isMainThread]) {
        [[ZYPopLayerManager sharedInstance] addRequest:self.request];
    }
    else {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[ZYPopLayerManager sharedInstance] addRequest:self.request];
        });
    }
}

- (void)cancel
{
    if ([NSThread isMainThread]) {
        [[ZYPopLayerManager sharedInstance] removeRequest:self.request];
        self.request = nil;
    }
    else {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[ZYPopLayerManager sharedInstance] removeRequest:self.request];
            self.request = nil;
        });
    }
}
@end
